package cn.itcast.service.impl;

import cn.itcast.domain.User;
import cn.itcast.mapper.UserMapper;
import cn.itcast.service.UserService;
import com.github.pagehelper.PageHelper;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import java.util.List;

/**
 * 业务逻辑层
 */
public class UserServiceImpl implements UserService {
        private static  SqlSession sqlSession = null;
        private static  UserMapper mapper =  null;
        static {

            try {
                //读取核心配置文件,并获取sqlsessoinfactory工厂对象
                SqlSessionFactory sessionFactory = new SqlSessionFactoryBuilder().build(Resources.getResourceAsStream("SqlConfig.xml"));
                //获取sqlsession对象
                 sqlSession = sessionFactory.openSession(true);
                //获取接口代理对象
                 mapper = sqlSession.getMapper(UserMapper.class);
            }catch (Exception e){
                e.printStackTrace();
            }


        }


    /*查询所有*/
    @Override
    public List<User> findAll(Integer pageNum) {
        //分页查询
        PageHelper.startPage(pageNum,3);

        return mapper.findAll();
    }

    /*根据用户名和密码查询用户*/

    @Override
    public User findById(String username, String password) {

            return mapper.findById(username,password);
    }


}
